package query

import (
	"fmt"
	"strings"

	"gitee.com/sansaniot/ssiot-core/facade/env"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"github.com/gin-gonic/gin"

	"ssadmin/common/constant"
)

type UserInfo struct {
	UserId     string   `search:"-"`
	UserName   string   `search:"-"`
	DeptIdList []string `search:"-"`
	DeptId     string   `search:"-"`
	AgencyId   string   `search:"-"`
}

func (e *UserInfo) LoadUser(c *gin.Context) {
	username := user.Get(c, constant.CacheKeyUserName).(string)
	key := fmt.Sprintf("login_user:%s", username)
	userInfo, _ := env.Cache.HashGetAll(key)
	e.UserId = userInfo[constant.CacheKeyUserId]
	e.UserName = username
	e.DeptIdList = strings.Split(userInfo[constant.CacheKeyUserDeptIds], ",")
	if e.UserId == "1" {
		e.AgencyId = "1"
	} else {
		e.AgencyId = userInfo[constant.CacheKeyAgencyId]
	}
}
